/* =========================================================================
 *
 *   The traffic processor.
 *
 *   Development by Sedelnikov Alexander (Bigking).
 *
 * =========================================================================
 */

#ifndef __THREAD_H__
#define __THREAD_H__


#include "common.h"
#ifdef _WIN32
#include <windows.h>
#include <winbase.h>
#else
#include <pthread.h>
#endif


//-----------------------------------------------------------------------//
#ifdef _WIN32
extern DWORD WINAPI __thread_call_function( void  *ptr );
#else
extern void* __thread_call_function( void  *ptr );
#endif


//-----------------------------------------------------------------------//
class cthread
{


private:
#ifdef _WIN32
    HANDLE       thread_id;
#else
    pthread_t    thread_id;
#endif


public:
    cthread()
    {
#ifdef _WIN32
        thread_id = NULL;
#else
        thread_id = 0;
#endif
    }
#ifdef _WIN32
    DWORD        get_thread_id() const;
#else
    pthread_t    get_thread_id() const;
#endif

    int          thread_run( void );
    void         thread_close( void );

    virtual void thread_running( void ) = 0;


};


//-----------------------------------------------------------------------//
class cprocess
{


public:
    cprocess() {}
    ~cprocess() {}

    static int    get_pid();
#ifdef _WIN32
    static bool   pid_exist( DWORD id );
#else
    static bool   pid_exist( ulong id );
#endif


};


//-----------------------------------------------------------------------//
class ccritical_section
{

#ifdef _WIN32
    CRITICAL_SECTION             c_section;
#else
#endif


private:

    static bool                  section_init;
    static ccritical_section     singleton;

    ccritical_section();
    ~ccritical_section();

public:

    static void enter();
    static void leave();


};


#endif
